/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with AdminOverlay.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#pragma once

#define DIRECTINPUT_VERSION 0x0800
#include <Windowsx.h>
#include "Dinput.h"
#include <vector>
#include "OverlayConfig.h"
#include "detours.h"
#include "CEGUI.h"

using namespace std;

class GameOverlay;

class InputManager
{
public:
	InputManager(GameOverlay *overlay);
	static void initalize(HINSTANCE instance, DWORD threadID);
	POINT MakePoint(LPARAM param);
	static InputManager *currentInstance;
	bool getMessageHandler(int code, WPARAM wParam, LPARAM lParam);
	~InputManager(void);
	void Start()
	{
		isStarted = true;
	}
public:
	void setupDetours();
private:
	bool isCtrlDown;
	//ignore input except ctrl-activationkey
	bool deliverMessageWithoutHook;
	GameOverlay *overlay;
	int activationKey;
	//makes the directinput thing zero the keys
	bool releaseAllKeys;
	bool isStarted;
	POINTS mousePos;
private:	
	bool onKeyDown_Msg(MSG *msg);
	bool onKeyUp_Msg(MSG *msg);
	bool onMouseMove_Msg(MSG *msg);
	bool onMouseDown_Msg(MSG *msg);
	bool onMouseUp_Msg(MSG *msg);

private:
	//direct input stuff
	//hooks
	static HRESULT __stdcall My_GetDeviceData
		(LPDIRECTINPUTDEVICE8, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD);
	static HRESULT __stdcall My_GetDeviceState
		(LPDIRECTINPUTDEVICE8, DWORD, LPVOID);
	//trampolines
	static HRESULT (__stdcall *GetDeviceData_Trampoline)
		(LPDIRECTINPUTDEVICE8, DWORD, LPDIDEVICEOBJECTDATA, LPDWORD, DWORD);
	static HRESULT (__stdcall *GetDeviceState_Trampoline)
		(LPDIRECTINPUTDEVICE8, DWORD, LPVOID);
	//setup functions
	void setupGetDeviceDataDetours(int offset);
	void setupGetDeviceStateDetours(int offset);
	//controller function
};
static HHOOK getMessageHook;
static LRESULT CALLBACK getMSGProc(int code, WPARAM wParam, LPARAM lParam);